package com.coin900.coin.cmd.system_config;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.sbi.SbiSystemConfigVO;
import com.coin900.coin.service.sbi.ISbiSystemConfigService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 修改项目
 *
 * @author shui
 * @create 2017-11-29
 **/
@Controller
public class SystemConfigPatchCmd extends ACmd{

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {

        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        JSONArray jsonArray = JSONArray.fromObject(source);
        List<SbiSystemConfigVO> sbiSystemConfigVOS=new ArrayList<>();
        for (int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            SbiSystemConfigVO configVO = (SbiSystemConfigVO)JSONObject.toBean(jsonObject,SbiSystemConfigVO.class);
            configVO.setModifiedInfor(user);
            sbiSystemConfigVOS.add(configVO);
         }

        systemConfigService.updateSbiSystemConfig(sbiSystemConfigVOS);
        return respSign;
    }

    @Autowired
    private ISbiSystemConfigService systemConfigService;

    @Override
    public String getCmdCode() {
        return CmdCode.SYSTEM_CONFIG_PATCH;
    }
}
